﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace BD2MKV
{
    public partial class ProgressBarText : ProgressBar
    {
        public ProgressBarText()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
        }

        private string label = "";

        [Browsable(true)]
        [Description("Sets de label to be used by the control to prefix/suffix the percent progress text")]
        public String Label
        {
            get { return label; }
            set
            {
                label = value;
                this.Invalidate();
            }
        }

        public enum LabelPositions
        {
            Prefix, // default
            Suffix
        }

        private LabelPositions pos = LabelPositions.Prefix;

        [Browsable(true)]
        [Description("Determines where to put the label, before or after the percent text")]
        public LabelPositions LabelPosition
        {
            get { return this.pos; }
            set
            {
                this.pos = value;
                this.Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            double pct;
            pct = (double)this.Value / (double)10;
            // call to the base class OnPaint method
            base.OnPaint(e);

            // custom painting here
            Graphics g = e.Graphics;

            using (Brush fontBrush = new SolidBrush(Color.Black))
            {
                using (StringFormat sf = new StringFormat())
                {
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment = StringAlignment.Center;
                    Rectangle rect = this.ClientRectangle;
                    ProgressBarRenderer.DrawHorizontalBar(g, rect);
                    rect.Inflate(-3, -3);
                    if (this.Value > 0)
                    {
                        Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)this.Value / this.Maximum) * rect.Width), rect.Height);
                        ProgressBarRenderer.DrawHorizontalChunks(g, clip);
                    }
                   
                    switch (pos)
                    {
                        case LabelPositions.Prefix:
                            g.DrawString(label + string.Format("{0:0.0}%", pct), this.Font, fontBrush, this.ClientRectangle, sf);
                            break;
                        case LabelPositions.Suffix:
                            g.DrawString(string.Format("{0:0.0}%", pct) + label, this.Font, fontBrush, this.ClientRectangle, sf);
                            break;

                    }

                }
            }


        } 
    }
}
